tongfusoft.userUpdate = function() {
}

tongfusoft.userUpdate.prototype = {
		//提交用户修改
		dataSubmit : function() {

			$.validator.setDefaults({
				submitHandler : function() {
					var initData = common.getData("updateUser-from");
					var opr = $("#opr").val();
					var pwd = $("#password").val();
					var repwd = $("#againPassword").val();
					if(opr=='repassword' && pwd!=repwd ){
						document.getElementById("errorMsg").innerHTML = "两次密码输入不一致，请重新输入！";
					}else if(opr=='repassword' && pwd == repwd ){
						common.ajaxSubmitOnSuccess(initData, 'manager/user/currentUserEdit', function(data) {
							if (data.state == 'success') {
								toastr["success"]('密码已修改，请重新登录!');
								window.location.href=common.getContextPath('login');
							} else{
								toastr["error"]('保存失败!');
							}
						})
					}else{
						common.ajaxSubmitOnSuccess(initData, 'manager/user/edit', function(data) {
							if (data.state == 'success') {
								toastr["success"]('保存成功!');
								
								window.location.href=common.getContextPath('manager/user/listPage');
							}else{
								toastr["error"]('保存失败!');
							}
						})
					}
					
				}
			});
			$("#updateUser-from").validate();

			$("#submitBtn").click(function() {
				$("#updateUser-from").submit();
			})
			
			$("#rePassword").click(function() {
				var userId = $("#userId").val();
				swal({
				title : "是否确认?",
				text : "",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确认",
				cancelButtonText : "取消",
				closeOnConfirm : true
			}, function() {
				common.ajaxSubmitOnSuccess({
					"userId" : userId
				}, 'manager/user/rePassword', function(data) {

					if (data.state == 'success') {
						toastr["success"]('密码已重置!');
					}else {
						toastr["error"]('密码未重置!');
					}

				})
			});
			})
			$("#userListPage").click(function() {
				window.location.href=common.getContextPath('manager/user/listPage');
			})
		},
		//获取数据
		getUserDetail : function(){
			common.post('manager/user/detail/' + $("#userId").val(), {}, function(data) {
				if (data.state == 'success') {
					$('#loginName').val(data.data.loginName);
					$('#displayName').val(data.data.displayName);
					$('#email').val(data.data.email);
					$('#mobile').val(data.data.mobile);
					$('#createdBy').val(data.data.createdBy);
					$('#createdDate').val(data.data.createdDate);
					$('#lastUpdBy').val(data.data.lastUpdBy);
					$('#lastUpdDate').val(data.data.lastUpdDate);
				}else{
					toastr["error"]('未知错误，请联系管理员!');
				}
			});
		},
		//角色列表(用户未关联角色)
		roleTable : function() {
			common.post('manager/roleList/query', {
				"userId" : $("#userId").val()
			}, function(data) {
				$("#roleData").html('');
				var roleData='';
				if (data.state == 'success') {
					
					$.each(data.data, function(index, item) {
						var roleHtml =        ' <li style="clear:both;list-style-type: none">';
						roleHtml = roleHtml + ' <div class="col-md-1" style="width:50px;">';
						roleHtml = roleHtml +       '<div class="checkbox c-checkbox">';	
						roleHtml = roleHtml +        '<label><input type="checkbox" name="choiceRole" value="'+item.roleId+'"/> <span class="fa fa-check"></span></label>'
						roleHtml = roleHtml +       '</div>';	
						roleHtml = roleHtml + '</div>';		
						roleHtml = roleHtml + '<div class="col-md-6">';	
						roleHtml = roleHtml +    '<div style="padding-top:7px;">'+item.roleName +'</div>';	
						roleHtml = roleHtml + '</div>';	
						roleHtml = roleHtml + '</li>';	
						
						roleData =roleData+roleHtml;
					});
				}
				$("#roleData").html(roleData);
			});
		},
		
		
		//角色与用户权限列表(用户已关联角色)
		roleHasUsersTable : function() {
			common.post('manager/roleHasUsers/query', {
				"userId" : $("#userId").val()
			}, function(data) {
				$("#userHasRole").html('');
				var roleData='';
				if (data.state == 'success') {
					$.each(data.data, function(index, item) {
						var roleHtml =        '<tr style="text-align: center;">';
						roleHtml = roleHtml + '<td class="col-md-6">'+item.roleName +'</td>';
						roleHtml = roleHtml + '<td class="col-md-6">';
						roleHtml = roleHtml + '<button ruId="'+ item.ruId +'"';
						roleHtml = roleHtml + '" onclick="new tongfusoft.userUpdate().delroelHasUsers(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>';
						roleHtml = roleHtml + '</td>';
						roleHtml = roleHtml + '</tr>';	
						roleData =roleData+roleHtml;
					});
				}
				$("#userHasRole").html(roleData);
				
			});
		},
		//提交角色和用户权限操作
		roleSubmit : function(){
			$.validator.setDefaults({
				submitHandler : function() {
					var initData = common.getData("roleForm");
					initData['userId'] = $("#userId").val();
					var url = 'manager/roleHasUsers/add';

					common.ajaxSubmitOnSuccess(initData, url, function(data) {

						if (data.state == 'success') {
							$('#myModal').modal('hide');
							new tongfusoft.userUpdate().roleHasUsersTable();
							new tongfusoft.userUpdate().roleTable();
							toastr["success"]('保存成功！');
						} else {
							toastr["error"]('添加角色权限失败!');
						}

					})
				}
			});
			$("#roleForm").validate();

			$("#submitRole").click(function() {
				    var roleId = "";
					var temp = "";
					var choiceRole = document.getElementsByName("choiceRole");
					for ( var i = 0; i < choiceRole.length; i++) {
						if (choiceRole[i].checked) {
						   temp = choiceRole[i].value;
						   roleId = roleId + "," +temp;
						}
					}
					$("#roleIdList").val(roleId)
					
				$("#roleForm").submit();

			})
		},	
		//删除角色与用户绑定
		delroelHasUsers : function(obj){
			swal({
				title : "确认删除?",
				text : "",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "删除",
				cancelButtonText : "取消",
				closeOnConfirm : true
			}, function() {
				common.ajaxSubmitOnSuccess({
					"ruId" : $(obj).attr("ruId")
				}, 'manager/roleHasUsers/del', function(data) {

					if (data.state == 'success') {
						new tongfusoft.userUpdate().roleHasUsersTable();
						new tongfusoft.userUpdate().roleTable();
						toastr["success"]('删除成功！');
					}else {

						toastr["error"]('删除失败!');
					}

				})
			});
		},
		
		
		//全选
		AllCheck : function(){
			var allRoleCheck = document.getElementsByName("choiceRole");
			var checkRoleAll = document.getElementById("checkRoleAll");
			$("#checkRoleAll").click(function() {
				if(checkRoleAll.checked == true) {
					for(var i = 0; i < allRoleCheck.length; i++){
						allRoleCheck[i].checked = true;
					}
				 } else {
					 for(var i = 0; i < allRoleCheck.length; i++){
						 allRoleCheck[i].checked = false;
						}
				 }
			});

		}
}

$(function(){
	var user = new tongfusoft.userUpdate();
	 user.roleTable();
	 user.getUserDetail();
	 user.roleHasUsersTable();
	 user.dataSubmit();
	 user.roleSubmit();
	 user.AllCheck();
})